package com.beamfield.controller.manage.process;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.ProcDadoGroup;
import com.beamfield.service.manage.process.ProcDadoGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 台座班组绑定
 */
@Controller
@RequestMapping(value = "/cms/process/dadogroup")
public class ProcessDadoGroupController extends BaseController {
    private final ProcDadoGroupService procDadoGroupService;

    @Autowired
    public ProcessDadoGroupController(ProcDadoGroupService procDadoGroupService) {
        this.procDadoGroupService = procDadoGroupService;
    }
    @RequestMapping("/dadogroup")
    public ModelAndView index(){
        return new ModelAndView();
    }
    /**
     * 保存台座班组绑定
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request, ProcDadoGroup procDadoGroup){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(procDadoGroup.getDado_id())||StringUtils.isEmpty(procDadoGroup.getGroup_id())){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        procDadoGroup.setAddtime(new Date());
        procDadoGroup.setBeam_id(beamId);
        procDadoGroup.setOperator(userId);
        int result = 0;
        try{
            result = procDadoGroupService.save(procDadoGroup);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"新增成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 删除台座班组绑定
     * @param request
     * @return
     */
    @RequestMapping(value = "/doDelete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doDelete(HttpServletRequest request, String procDadoGroupId){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        int result = 0;
        try{
            result = procDadoGroupService.delete(procDadoGroupId ,beamId ,userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"删除成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 查询工序审核人员配置
     * @param request
     * @return
     */
    @RequestMapping(value = "/readProcDadoGroup",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readProcDadoGroup(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        List<ProcDadoGroup> procDadoGroupList = procDadoGroupService.getProcDadoGroup(beamId, offset, limit);
        long total = procDadoGroupService.countByBeamId(beamId);
        return new PageResult(total, Long.parseLong(offset), procDadoGroupList);
    }
}
